const Koa = require('koa');
const router = require('./routers/api')
const cors = require('koa2-cors');
const bodyParser = require('koa-bodyparser');
const jwt = require('jsonwebtoken')
const tcpServer = require('../mbserver/tcpserver/newTcpserver')
//引入串口服务器
const SerialToTcp = require('../mbserver/serialport/serialToTcp')



//运行tcpServer
tcpServer()

//运行串口服务器
const serialServer = new SerialToTcp
/*
serialServer.newConnect({})  传入配置对象支持一次传入多个
[{},{},{}]
接受参数  {port:服务器端口号  com:'' 电脑端口 deviceId:'' 注册设备号(不能重复)}

*/ 
// serialServer.newConnect({port:15000,com:'com5',deviceId:'1234381162797123400002'})




const app = new Koa();
const secret = 'iioott';

const handler = async (ctx, next) => {
    try {
        await next();
    } catch (err) {
        console.log(err);
        ctx.body = { status: 1 }
    }
};

app.use(handler);

app.use(bodyParser()).use(router.routes())
    .use(router.allowedMethods())
    .use(cors())




app.listen(3001, () => {
    console.log('server  is  running  at : http://localhost:3001')
})
